package com.edu.dao;

import com.edu.pojo.EduTeacher;
import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * 教师数据库操作层
 *
 * @author YJF
 */
public interface EduTeacherMapper extends Mapper<EduTeacher> {

    /**
     * 查询此分院的教师人数
     *
     * @param deptId 分院ID
     * @return 教师人数
     */
    @Select("SELECT COUNT(*) FROM edu_teacher WHERE dept_id = #{deptId};")
    Integer selectTecCount(Integer deptId);

    /**
     * 查询此分院的负责人
     *
     * @param tecId 教师ID
     * @return 负责人名字
     */
    @Select("SELECT tec_name FROM edu_teacher WHERE tec_id = #{tecId};")
    String findLeader(Long tecId);

    /**
     * 通过教师姓名查找所有教师ID
     *
     * @param curBy 教师姓名
     * @return 返回所有教师ID的集合
     */
    @Select("SELECT tec_id FROM edu_teacher WHERE tec_name LIKE '%${curBy}%'")
    List<Integer> selectIdByName(@Param("curBy") String curBy);

    /**
     * 根据教师ID查询用户信息
     *
     * @param id 教师ID
     * @return 查询到的用户信息
     */
    @Select("SELECT " +
            "t.tec_id, t.tec_name, t.tec_user, t.tec_per_id, data_scope " +
            "FROM edu_teacher t LEFT JOIN edu_role r ON r.role_id = t.role_id WHERE tec_id = #{tecId}")
    @Results({
            @Result(column = "tec_id", property = "tecId"),
            @Result(column = "tec_name", property = "tecName"),
            @Result(column = "tec_user", property = "tecUser"),
            @Result(column = "tec_per_id", property = "tecPerId"),
            @Result(column = "data_scope", property = "eduRole.dataScope")
    })
    EduTeacher findRoleById(@Param("tecId") Integer id);

    /**
     * 根据教师ID重置密码
     *
     * @param id 教师ID
     * @return 返回响应行数
     */
    @Update("UPDATE edu_teacher SET tec_pwd = '123456' WHERE tec_id = #{tecId}")
    int rePwd(@Param("tecId") Integer id);

    /**
     * 根据教师ID删除教师用户
     *
     * @param eduTeacher 条件
     * @return 返回响应行数
     */
    @Update("UPDATE edu_teacher SET tec_user = '', tec_pwd= '', role_id = NULL, update_by = #{tecName}, update_time = #{updateTime} WHERE tec_id = #{tecId}")
    int delPower(EduTeacher eduTeacher);

}
